package com.atguigu.edu.service.impl;

import com.atguigu.edu.entity.EduChapter;
import com.atguigu.edu.entity.EduSection;
import com.atguigu.edu.mapper.EduChapterMapper;
import com.atguigu.edu.service.EduChapterService;
import com.atguigu.edu.service.EduSectionService;
import com.atguigu.exception.EduException;
import com.atguigu.response.ChapterVO;
import com.atguigu.response.SectionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author HB
 * @since 2020-11-10
 * 之所以可以在xxxImpl类中直接使用baseMapper 是因为extends ServiceImpl 。ServiceImpl里面有
 * 注入了protected M baseMapper;  所以在子类中可以直接使用
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduSectionService sectionService;
    @Override
    public boolean saveChapter(EduChapter chapter) {
        //判断是否存在
        EduChapter existChapter = existChapter(chapter.getCourseId(), chapter.getTitle());
        if(existChapter==null){
            int insert = baseMapper.insert(chapter);
            return insert>0;
        }else{
            throw new EduException(20001,"章节已经重复");
        }
    }

    public EduChapter existChapter(String courseId, String chapterName) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.eq("title", chapterName);
        EduChapter chapter = baseMapper.selectOne(queryWrapper);
        return chapter;

    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //判断是否有小节
        QueryWrapper<EduSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = sectionService.count(queryWrapper);
        //没有小节
        if(count==0){
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }else{
            throw new EduException(20001,"该章节存在小节");
        }
    }

    /**
     * 根据课程的id 查询chapter章节信息 和section小结的信息
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVO> getChapterAndSection(String courseId) {
        //查询所有的章节信息
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(chapterWrapper);

        //查询所有的小结信息
        QueryWrapper<EduSection> eduSectionQueryWrapper = new QueryWrapper<>();
        eduSectionQueryWrapper.eq("course_id",courseId);
        List<EduSection> eduSectionList = sectionService.list(eduSectionQueryWrapper);

        //把所有的小结信息塞到对应的章节信息里
        List<ChapterVO> chapterVOList = new ArrayList<>();
        for (EduChapter eduChapter : eduChapterList) {
            ChapterVO chapterVO = new ChapterVO();
            //将数据库中查询的信息封装到VO中
            BeanUtils.copyProperties(eduChapter,chapterVO);
            for (EduSection eduSection : eduSectionList) {
                if(eduSection.getChapterId().equals(eduChapter.getId())){
                    SectionVO sectionVO = new SectionVO();
                    //将数据库中查询的信息封装到VO中
                    BeanUtils.copyProperties(eduSection,sectionVO);
                    chapterVO.getChildren().add(sectionVO);
                }

            }
            chapterVOList.add(chapterVO);
        }
        return chapterVOList;
    }

    /**
     * 根据课程id删除章节信息
     * @param courseId
     */
    @Override
    public void deleteChapterByCourseId(String courseId) {
        //在chapter表中 将courseId作为条件 删除chapter表的信息
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
